<?php
namespace Home\Controller;
use Think\Controller;
class BookController extends Controller {
	
	
	//阅读书列表
    public function index(){
    	$book_id = I('get.book_id','');
    	
    	if (empty($book_id)) {
    		$this->error('没有找到此书');
    	}else {
    		$Model = new \Think\Model\MongoModel($book_id);
    		$book_count = $Model->count();// 查询满足要求的总记录数
    		include MODULE_PATH.'Common/'.'Page.class.php';
    		$Page       = new \Page($book_count,500);// 实例化分页类 传入总记录数和每页显示的记录数(25)
    		$show       = $Page->show();// 分页显示输出
    		$this->assign('page',$show);// 赋值分页输出
    			
    		$list = $Model->order('_id asc')->limit($Page->firstRow.','.$Page->listRows)->select();
    		$this->assign('list',$list);
    		$this->assign('id',$book_id);
    		$book = D('Book')->find_book($book_id);
    		$this->assign('book',$book[$book_id]);
    		$this->assign('book_count',$book_count);
    	}
    	
	    $this->display();
    }
	
    
	//图书
	public function read()
	{
		$book_id = I('get.book_id','');
		$article_id = I('get.article_id','');
		if(empty($book_id) || empty($article_id))
		{
			$this->error('没有找到此文章');
		}else
		{
			$Model = new \Think\Model\MongoModel($book_id);
			
			$book_count = $Model->count();// 查询满足要求的总记录数
			include MODULE_PATH.'Common/'.'Page.class.php';
			$Page       = new \Page($book_count,500);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show       = $Page->show();// 分页显示输出
			$this->assign('page',$show);// 赋值分页输出
			$list = $Model->order('_id asc')->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('list',$list);
			$book = D('Book')->find_book($book_id);
			$this->assign('book',$book[$book_id]);
			$this->assign('book_count',$book_count);
			
			
			$theBook = array('_id'=>new \MongoId($article_id));
			//查找书
			$article = $Model->where($theBook)->select();
			$this->assign('article',$article[$article_id]);
			$this->assign('book_id',$book_id);
			$this->assign('article_id',$article_id);
			//dump($list);
			
			//获取前一章和后一章的id
			$pre['_id'] = array('lt',new \MongoId($article_id));
			$res = $Model->field('_id,title')->where($pre)->order('_id desc')->find();
			$preuuid = empty($res) ? '' :$res['_id'];
			$next['_id'] = array('gt',new \MongoId($article_id));
			$res = $Model->field('_id,title')->where($next)->order('_id asc')->find();
			$nextuuid = empty($res) ? '' :$res['_id'];
			$this->assign('preuuid',$preuuid);
			$this->assign('nextuuid',$nextuuid);
			
			$this->display();
			
		}
		
	}
	
	//java 返回json数据
	public function Bookreader()
	{	
		$book_id = I('get.book_id','');
		$article_id = I('get.article_id','');
		
		if(empty($book_id) || empty($article_id))
		{
			$this->error('没有找到此文章');
		}else
		{
			//"/615242/4?fontsize=14&lang=zhs&w=830";
			$Model = new \Think\Model\MongoModel($book_id);
			
			
			//查找书
			$theBook = array('_id'=>new \MongoId($article_id));
			$article = $Model->where($theBook)->select();
			$this->assign('article',$article[$article_id]);
			$this->assign('book_id',$book_id);
			$this->assign('article_id',$article_id);
			
			//print_r($article);
			
			$pre['_id'] = array('lt',new \MongoId($article_id));
			$res = $Model->field('_id,title')->where($pre)->order('_id desc')->find();
			$preuuid = empty($res) ? '' :$res['_id'];
			$next['_id'] = array('gt',new \MongoId($article_id));
			$res = $Model->field('_id,title')->where($next)->order('_id asc')->find();
			$nextuuid = empty($res) ? '' :$res['_id'];
			$this->assign('preuuid',$preuuid);
			$this->assign('nextuuid',$nextuuid);
			
			$data['content']	= $this->fetch('content');
			$data['preuuid'] 	= $preuuid;
			$data['nextuuid'] 	= $nextuuid;
			$data['nextIsVip'] 	= '0';
			$data['uuid'] 		= $article_id;
			$data['hasTaked'] 	= rand();
			$data['showReward'] = '0';
			$this->ajaxReturn($data);
		}
		
		
	}
	
	
}